node1Index = 3
-------------- 茶壶类 ---------------
--建立teapot
Teapot = {
    index = -1, --序号
    position = _Vector3.new(), --随机位置
    scale = _Vector3.new(5, 5, 5), --大小
    mesh = _mf:createTeapot(), --返回 _Mesh
    matTeapot = _Matrix3D.new() -- mesh的变换矩阵
}
--随机位置
function Teapot:randomPosition()
    x, y, z = _random(-50, 50), _random(-50, 50), 0
    move.position:set(x, y, z)
    self.position:set(x, y, z)
end

--改变茶壶的大小
function Teapot:changeScale(xScale, yScale, zScale)
    self.scale:set(xScale, yScale, zScale)
    self.mesh.transform:setScaling(self.scale)
end

--创建新的茶壶实例
function Teapot:new(scene, index)
    local o = {}
    setmetatable(o, {__index = self})
    o.index = index
    --改变位置
    o:randomPosition()
    --改变大小
    o.matTeapot:setScaling(self.scale)
    --壶底中心移动原点的位移
    local zDistance = scene.terrain:getHeight(o.position.x, o.position.y) + o.scale.z
    local matTeapot = o.position:add(0, 0, zDistance)
    move.position.z = zDistance
    o.matTeapot:mulTranslationRight(matTeapot)--位移
    
    o.mesh.transform:set(o.matTeapot) --transform mesh的变换矩阵
    return o
end

--将茶壶加入场景
function addTeapotToScene(scene, nodeIndex)
    local teapot = Teapot:new(scene, nodeIndex)
    scene:add(teapot.mesh) --作为节点加入
end

--------------- 移动 旋转------------
move = {
    direction = _Vector3.new(-1, 0, 0),
    position = _Vector3.new(0, 0, 0)
}
--改变mesh的变换矩阵
function changeMatTeapot(teapotMat, result, zNewPosition) 
    -- 旋转
    local newPosition = _Vector3.new(result.x, result.y, zNewPosition)
    local oldPosition = teapotMat:getTranslation()

    local direction = _Vector3.sub(newPosition, oldPosition)
    direction.z = 0 --方向水平
    teapotMat:mulFaceToLeft(move.direction, direction, 200) -- old->new
    move.direction = direction
    -- 移动
    move.position = newPosition
    return teapotMat
end
--移动
function update(scene, move, e)
    local oldPosition = scene:getNode(node1Index).mesh.transform:getTranslation()
	if oldPosition:equal(move.position) then
		return
	end
	distance = _Vector3.sub(move.position, oldPosition)
	if distance:magnitude() > 0.1 * e then
		distance = distance:scale(0.1 * e)
    end

    -- 始终在面上
    local matTem = _Matrix3D.new()
    matTem:set(scene:getNode(node1Index).mesh.transform)
    matTem:mulTranslationRight(distance)
    local positionTem = matTem:getTranslation()
    positionTem.z = scene.terrain:getHeight(positionTem.x, positionTem.y) + scene:getNode(node1Index).mesh.transform:getScaling().z
    distance = _Vector3.sub(positionTem, oldPosition)

	scene:getNode(node1Index).mesh.transform:mulTranslationRight(distance)
end
--------------- 场景 ------------
--载入场景
_sys:addPath('res')
local sen = _Scene.new('terrain.sen')

addTeapotToScene(sen, node1Index)

_app:onMouseDown(function(button, x, y) --相对于窗口的坐标
    if(button == 0) then
        local teapotMat = sen:getNode(node1Index).mesh.transform

        local teapotMatTem = sen:getNode(node1Index).mesh.transform

        local result = sen:pick(_rd:buildRay(x, y)) --转变为相对于场景原点的坐标
        zScale = teapotMat:getScaling().z
        local zNewPosition = sen.terrain:getHeight(result.x, result.y) + zScale --该点高度 + 茶壶高度
        changeMatTeapot(teapotMat, result, zNewPosition) --更改变换矩阵
    end
end)

_app:onIdle(function(e)
    _rd:drawAxis(200)
    sen:render()
    update(sen, move, e)
end)